local RxFindPng = {}

-- 误差找图 - 返回 x,y
function RxFindPng:findPngAreaTable(url, areaTable, deviation)
  if not areaTable then
    RxLog('RxFindPng:findPngAreaTable areaTable = nil')
    return -1,-1
  end
  local x1, y1, x2, y2 = areaTable[1],areaTable[2],areaTable[3],areaTable[4]
  deviation = deviation or 20
  return findImage(url, x1, y1, x2, y2, deviation)
end

-- 二值化找图 - 返回 x,y
function RxFindPng:findPngAreaTableInRegionFuzzy(url, areaTable, degree, kind)
  if not areaTable then
    RxLog('RxFindPng:findPngAreaTableInRegionFuzzy areaTable = nil')
    return -1,-1
  end
  local x1, y1, x2, y2 = areaTable[1],areaTable[2],areaTable[3],areaTable[4]
  degree = degree or 90
  kind = kind or 2
  -- 测试
  if developEnv == 0 then
    local imgName = UtilImageNamePath('RxFindPngFuzzy')
    snapshot(imgName, x1, y1, x2, y2)
  end
  return findImageInRegionFuzzy(url, degree, x1, y1, x2, y2, 0, kind)
end

-- 图片 x1,y1,x2,y2
-- 图片table 元素
-- 误差
function RxFindPng:findPngTable(imgUrlRegion, assetIconTable, deviation)
  deviation = deviation or 20
  function fun(url, uTabTitle, x1, y1, x2, y2, RxValue)
    return findImage(url, x1, y1, x2, y2, deviation)
  end
  local index, resTab = RxFindPng:findPngLoop(imgUrlRegion, assetIconTable, fun)
  return index, resTab
end

--[[
  目前实践成功： 敦煌 - 精巧的机关、 神秘的箱子
  --
  -- 先用 [RxPngDiff:imgUrlTable] 截取小图，最小尺寸的图 (黑白图)
  -- 然后 再用最大尺寸的 4个坐标和 上面的 模糊比对
  --
]]--
function RxFindPng:findPngInRegionFuzzyTable(imgUrlRegion, assetIconTable, kind, degree)
  degree = degree or 90
  kind = kind or 0
  function fun(url, uTabTitle, x1, y1, x2, y2, RxValue)
    -- 测试
    if developEnv == 0 then
      local imgName = UtilImageNamePath('RxFindPngFuzzy')
      snapshot(imgName, x1, y1, x2, y2)
    end
    return findImageInRegionFuzzy(url, degree, x1, y1, x2, y2, 0, kind)
  end
  local index, resTab = RxFindPng:findPngLoop(imgUrlRegion, assetIconTable, fun)
  return index, resTab
end

function RxFindPng:findPngLoop(imgUrlRegion, assetIconTable, fun)
  local index = 0
  local x, y = -1, -1
  local iconIndex = 0
  if not imgUrlRegion then
    RxLog('RxFindPng:findPngTable imgUrlRegion = nil')
    return iconIndex
  end

  local iconLen = assetIconTable.length or 0
  if iconLen == 0 then
    RxLog('RxFindPng:findPngTable assetIconTable.length = 0')
    return iconIndex
  end

  local x1, y1, x2, y2 = imgUrlRegion[1],imgUrlRegion[2],imgUrlRegion[3],imgUrlRegion[4]

  local preResult = {}
  local preResultIndex = 1
  local num = 1

  while true do
    ::_restart::
    if num > iconLen then
      break
    end

    local uTab = assetIconTable.icon[num]
    local uTabUrl = uTab.url
    local url = UtilImageEnvPath(uTabUrl)
    local urlBool, urlKind = isFileExist(url)
    if not urlBool then
      num = num + 1
      RxLog('RxFindPng:findPngTable 加载 本地图片失败，是否路径正确\n uTabUrl='.. uTabUrl .. ' url='..url)
      goto _restart
    end

    local uTabTitle = uTab.title
    local RxValue = uTab.value
    x,y = fun(url, uTabTitle, x1, y1, x2, y2, RxValue)
    -- x,y = findImage(url, x1, y1, x2, y2, deviation)
    if x ~= -1 and y ~= -1 then
      RxLog('RxFindPng:findPngTable 成功找图 uTabTitle='..tostring(uTabTitle).. ' num=' ..tostring(num) ..' x='..tostring(x)..' y='..tostring(y))
      local tttt = uTab
      tttt.num = num
      tttt.x = x
      tttt.y = y
      table.insert(preResult, preResultIndex, tttt)
      preResultIndex = preResultIndex + 1
      index = uTab.index
    end

    num = num + 1
  end

  local re = tostring(preResult)
  RxLog('RxFindPng:findPngTable preResult=' .. re)
  return index, preResult
end


return RxFindPng